package com.cbank.controller.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cbank.domain.City;
import com.cbank.domain.Company;
import com.cbank.domain.Industry;
import com.cbank.domain.User;
import com.cbank.domain.UserCompany;
import com.cbank.service.CityService;
import com.cbank.service.CompanyService;
import com.cbank.service.IndustryService;
import com.cbank.service.UserCompanyService;
import com.cbank.service.UserService;
import com.cbank.utils.ExcelUtils;

/**
 * @author mac 筛选数据类
 */
@Controller
@RequestMapping("/web/data")
public class WebDataController {
	@Autowired
	CityService cityService;
	@Autowired
	IndustryService industryService;
	@Autowired
	UserCompanyService userCompanyService;
	@Autowired
	CompanyService companyService;
	@Autowired
	UserService userService;

	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String getIndustry(@CookieValue(value = "uid", defaultValue = "") long uid,HttpServletResponse response, Model model) {
		List<City> cities = cityService.getList(0,1000);
		List<Industry> industries = industryService.getList(0, 1000);
		User user = userService.getByViewId(uid);
		Long id = user.getId();
		List<UserCompany> list = userCompanyService.getListByUserId(id);//得到今日下载的数据集合
		List<Company> companies = new ArrayList<Company>(list==null?0:list.size());
		for (UserCompany userCompany : list) {
			long companyTableId = userCompany.getCompanyTableId();
			companies.add(companyService.getById(companyTableId));
		}
		long uidL = user.getId();
		int count = userCompanyService.getTotalCountByUserId(uidL);// 今天已经下载数量
		long canDownNum = user.getCanDownloadEachDay() - count;// 可下载数量
		model.addAttribute("count", count);//数据已下载量
		model.addAttribute("canDownNum", canDownNum);//数据可下载量
		model.addAttribute("user", user);//用户
		model.addAttribute("companies", companies);//今天下载的数据
		model.addAttribute("cities", cities);
		model.addAttribute("industries", industries);
		return "xiansuowajue";
	}
	@RequestMapping(value = "/excel", method = RequestMethod.GET)
	public void exportExcel(@CookieValue(value = "uid", defaultValue = "") long uid,HttpServletResponse response, Model model,HttpServletRequest request){
		User user = userService.getByViewId(uid);
		Long id = user.getId();
		List<UserCompany> list = userCompanyService.getListByUserId(id);//得到今日下载的数据集合
		List<Company> companies = new ArrayList<Company>(list==null?0:list.size());
		for (UserCompany userCompany : list) {
			long companyTableId = userCompany.getCompanyTableId();
			companies.add(companyService.getById(companyTableId));
		}
		try {
			ExcelUtils.exportExcel(companies, response, request, "线索_"+System.currentTimeMillis(),new ExcelUtils.Callback<Company>() {

				@Override
				public void setData(HSSFCell cell, int index, Company t) {
					
					
					
				}
			});
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
